package com.feng.chat.utils;

import com.feng.chat.common.Common;
import com.feng.chat.entity.*;
import com.feng.chat.service.*;
import com.feng.chat.service.impl.UsersServiceImpl;
import com.feng.chat.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Component
public class RecordsUtil {
    @Autowired
    private static ITalkRecordsFileService talkRecordsFileService;

    @Autowired
    private static ITalkRecordsForwardService talkRecordsForwardService;

    @Autowired
    private static ITalkRecordsCodeService talkRecordsCodeService;

    @Autowired
    private static ITalkRecordsVoteService talkRecordsVoteService;

    @Autowired
    private static IGroupNoticeService groupNoticeService;

    @Autowired
    private static ITalkRecordsLoginService talkRecordsLoginService;

    @Autowired
    private static ITalkRecordsInviteService talkRecordsInviteService;

    @Autowired
    private static ITalkRecordsLocationService talkRecordsLocationService;


    @Autowired
    private static ITalkRecordsService talkRecordsService;

    @Autowired
    private static IUsersService usersService;

    public static RecordsRowsVO encapsulateMessage(TalkRecords talkRecords) {
        RecordsRowsVO recordsRowsVO = new RecordsRowsVO();
        recordsRowsVO.setId(talkRecords.getId());
        recordsRowsVO.setIs_mark(talkRecords.getIsMark());
        recordsRowsVO.setIs_read(talkRecords.getIsRead());
        recordsRowsVO.setIs_revoke(talkRecords.getIsRevoke());
        recordsRowsVO.setMsg_type(talkRecords.getMsgType());
        recordsRowsVO.setReceiver_id(talkRecords.getReceiverId());
        recordsRowsVO.setTalk_type(talkRecords.getTalkType());
        recordsRowsVO.setCreated_at(talkRecords.getUpdatedAt().format(DateTimeFormatter.ofPattern(Common.LOCALDATETIME_FORMATER)));
        recordsRowsVO.setUser_id(talkRecords.getUserId());


        /*Users users = MessageUtil.usersService.getById(talkRecords.getUserId());

        recordsRowsVO.setAvatar(users.getAvatar());
        recordsRowsVO.setNickname(users.getNickName());*/
        switch (talkRecords.getMsgType()) {
            case 0:
            case 1:
                recordsRowsVO.setContent(talkRecords.getContent());
                break;
            case 2:
                TalkRecordsFile recordsType = (TalkRecordsFile) MessageUtil.getRecordsType(talkRecords);
                FileVO fileVO = new FileVO();
                fileVO.setCreated_at(recordsType.getCreatedAt().format(DateTimeFormatter.ofPattern(Common.LOCALDATETIME_FORMATER)));
                fileVO.setDrive(recordsType.getDrive());
                fileVO.setId(recordsType.getId());
                fileVO.setOriginal_name(recordsType.getOriginalName());
                fileVO.setPath(recordsType.getPath());
                fileVO.setRecord_id(recordsType.getRecordId());
                fileVO.setSize(recordsType.getSize());
                fileVO.setSource(recordsType.getSource());
                fileVO.setSuffix(recordsType.getSuffix());
                fileVO.setType(recordsType.getType());
                fileVO.setUrl(recordsType.getUrl());
                fileVO.setUser_id(recordsType.getUserId());

                recordsRowsVO.setFile(fileVO);
                break;
            case 3:
                TalkRecordsForward talkRecordsForward = (TalkRecordsForward) MessageUtil.getRecordsType(talkRecords);
                ForwardVO forwardVO = new ForwardVO();
                List<ForwardListVO> forwardListVOS = new ArrayList<>();
                List<String> recordsList = Arrays.asList(talkRecordsForward.getRecordsId().split(","));
                for (String record : recordsList) {
                    TalkRecords talkRecords2 = talkRecordsService.getById(Integer.parseInt(record));
                    Users users1 = usersService.getById(talkRecords2.getUserId());
                    ForwardListVO forwardListVO = new ForwardListVO(users1.getNickName(), MessageUtil.getMessageType(talkRecords2.getMsgType(), talkRecords2.getContent()));
                    forwardListVOS.add(forwardListVO);
                }
                forwardVO.setList(forwardListVOS);
                forwardVO.setNum(forwardListVOS.size());

                recordsRowsVO.setForward(forwardVO);
                break;
            case 4:
                TalkRecordsCode talkRecordsCode = (TalkRecordsCode) MessageUtil.getRecordsType(talkRecords);
                CodeBlockVO codeBlockVO = new CodeBlockVO();
                codeBlockVO.setCode(talkRecordsCode.getCode());
                codeBlockVO.setRecord_id(talkRecords.getId());
                codeBlockVO.setCreated_at(talkRecordsCode.getCreatedAt().format(DateTimeFormatter.ofPattern(Common.LOCALDATETIME_FORMATER)));
                codeBlockVO.setId(talkRecordsCode.getId());
                codeBlockVO.setLang(talkRecordsCode.getLang());
                codeBlockVO.setUser_id(talkRecordsCode.getUserId());
                recordsRowsVO.setCode_block(codeBlockVO);
                break;
        }
        return recordsRowsVO;
    }
}
